package app.ui;

import game.components.AppUtilities;
import game.components.GameAction;
import game.components.GameTimer;
import game.components.Player;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;

import remote.GameActionIfc;
import app.action.MouseAction;

public class PlayingScreenUi extends AbsCommonPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3794610936711464141L;

	protected PlayAreaPanel playAreaPanel;
	protected Timer timerControlGame;
	protected MouseAction mouseAction;
	protected JLabel[] jLabels;
	protected GameAction gameAction;
	// size = 14 -----------------------//"0", ------------1, ------------2,
	// -------------3, --------4, --------5, --------6, --------7,
	// ---------8,---------9, ----------10, --------11, -----12,
	// ---------------13
	protected final String[] lbl_text = { "X", "  Player 1:", "  Player 2:",
			"  Total sec:", "  Time:", "  Turn:", "score 1", "score 2",
			"amtOfsec", "countUp", "PlayerName", "NEW GAME", "STOP",
			"Message Screen:" };
	protected JTextArea jTextArea;
	protected JScrollPane jScrollPane;
	protected GameTimer gameTimer;

	public PlayingScreenUi(MainFrame mFrame) {
		super(mFrame);

		this.setBackground(new Color(100, 200, 255));
		this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
	}

	@Override
	protected void addComponents() {
		this.playAreaPanel = new PlayAreaPanel(this);
		add(playAreaPanel);

		int num = lbl_text.length;
		this.jLabels = new JLabel[num];
		for (int i = 0; i < num; i++) {
			jLabels[i] = new JLabel(lbl_text[i]);
			add(jLabels[i]);
		}

		jTextArea = new JTextArea();
		jScrollPane = new JScrollPane(jTextArea);
		add(jScrollPane);

		// set game action
		if (getAppUtilities().getGameAction() == null) {
			this.gameAction = new GameAction(this);
			getAppUtilities().setGameAction(gameAction);
		}
	}

	@Override
	protected void initializeComponents() {
		GameActionIfc gameActionIfc = getAppUtilities().getGameAction();
		this.mouseAction = new MouseAction(gameActionIfc);
		addMouseListener(mouseAction);
		addMouseMotionListener(mouseAction);
		playAreaPanel.addMouseListener(mouseAction);
		playAreaPanel.addMouseMotionListener(mouseAction);

		for (JLabel jLabel : jLabels) {
			// jLabel.addMouseListener(mouseAction);
			jLabel.setOpaque(true);
			jLabel.setBackground(Color.ORANGE);
		}
		int index = 0;
		// close label
		jLabels[index].addMouseListener(mouseAction);
		jLabels[index]
				.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		jLabels[index].setHorizontalAlignment(JLabel.CENTER);
		jLabels[index].setVerticalAlignment(JLabel.CENTER);
		jLabels[index].setBorder(BorderFactory.createLineBorder(Color.BLACK));
		jLabels[index].setBackground(new Color(200, 150, 250));

		// new game
		index = 11;
		jLabels[index].addMouseListener(mouseAction);
		jLabels[index]
				.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		jLabels[index].setHorizontalAlignment(JLabel.CENTER);
		jLabels[index].setVerticalAlignment(JLabel.CENTER);
		jLabels[index].setBorder(BorderFactory.createLineBorder(Color.BLACK));
		jLabels[index].setBackground(new Color(200, 150, 250));
		// Pause
		index = 12;
		jLabels[index].addMouseListener(mouseAction);
		jLabels[index]
				.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		jLabels[index].setHorizontalAlignment(JLabel.CENTER);
		jLabels[index].setVerticalAlignment(JLabel.CENTER);
		jLabels[index].setBorder(BorderFactory.createLineBorder(Color.BLACK));
		jLabels[index].setBackground(new Color(200, 150, 250));
		// message
		index = 13;
		jLabels[index].addMouseListener(mouseAction);
		// jLabels[index].setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		jLabels[index].setHorizontalAlignment(JLabel.CENTER);
		jLabels[index].setVerticalAlignment(JLabel.CENTER);
		jLabels[index].setBorder(BorderFactory.createLineBorder(Color.BLACK));
		jLabels[index].setBackground(new Color(200, 150, 250));

		// timer
		this.gameTimer = new GameTimer(jLabels[9]);

		// jScrollPane
		jScrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		jScrollPane
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		// jTextArea
		jTextArea.setAutoscrolls(true);
		jTextArea.setWrapStyleWord(true);

		// set information
		int count = 6;
		jLabels[count++].setText(String.valueOf(getAppUtilities().getPlayer1()
				.getAmtOfWin()));
		jLabels[count++].setText(String.valueOf(getAppUtilities().getPlayer2()
				.getAmtOfWin()));
		jLabels[count++].setText(String
				.valueOf(getAppUtilities().getTotalSec()));
		count++;
		jLabels[count++].setText(String.valueOf(getAppUtilities()
				.getTurnOfPlayer().getName()));
	}

	@Override
	protected void resizeComponents() {
		final int panelInfor = 159;
		int count = 0;
		int x = 0;
		int y = 0;
		int width = getWidth() - panelInfor;
		int height = getHeight();

		// play area
		playAreaPanel.setBounds(x, y, width, height);

		// information panel
		// close
		width = 25;
		height = 20;
		x = getWidth() - width;
		jLabels[count++].setBounds(x, y, width, height);
		// player 1
		x = getWidth() - panelInfor + 1;
		y = panelInfor / 2;
		width = panelInfor / 2;
		height = btnHei;
		jLabels[count++].setBounds(x, y, width, height);
		// player 2
		y += height;
		jLabels[count++].setBounds(x, y, width, height);
		// total section
		y += height;
		jLabels[count++].setBounds(x, y, width, height);
		// time
		y += height;
		jLabels[count++].setBounds(x, y, width, height);
		// turn
		y += height;
		jLabels[count++].setBounds(x, y, width, height);

		// score 1
		x += width;
		y = panelInfor / 2;
		width -= 2;
		jLabels[count++].setBounds(x, y, width, height);
		// score 2
		y += height;
		jLabels[count++].setBounds(x, y, width, height);
		// amount of section
		y += height;
		jLabels[count++].setBounds(x, y, width, height);
		// time count up
		y += height;
		jLabels[count++].setBounds(x, y, width, height);
		// turn
		y += height;
		jLabels[count++].setBounds(x, y, width, height);

		// new game
		x = getWidth() - panelInfor + 1;
		width = panelInfor / 2 - 2;
		y += (2 * height);
		jLabels[count++].setBounds(x, y, width, height);

		x += width + 1;
		jLabels[count++].setBounds(x, y, width, height);

		// message
		x = getWidth() - panelInfor + 1;
		y += (2 * height);
		width = panelInfor - 3;
		jLabels[count++].setBounds(x, y, width, height);
		// jScrollpane
		y += height;
		height = getHeight() - y - 1;
		jScrollPane.setBounds(x, y, width, height);
		jScrollPane.updateUI();
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public PlayAreaPanel getPlayAreaPanel() {
		return playAreaPanel;
	}

	public MouseAction getMouseAction() {
		return mouseAction;
	}

	public JLabel[] getjLabels() {
		return jLabels;
	}

	public String[] getLbl_text() {
		return lbl_text;
	}

	// ----------------------------------------------------------------
	// -- game control
	// ----------------------------------------------------------------
	public void addScore(Player player) {
		player.setAmtOfWin(player.getAmtOfWin() + 1);

		String value = String.valueOf(player.getAmtOfWin());

		int index = 5;
		index += player.getID();
		jLabels[index].setText(value);

		String message = player.getName() + " win!";
		JOptionPane.showMessageDialog(playAreaPanel, message);
		// pause game
		this.pausingGame();
	}

	public void setPlayerTurn() {
		// check winner
		if (getAppUtilities().getRuleOfGame().getWiner() != null) {
			this.addScore(getAppUtilities().getWinner());
		}

		getAppUtilities().changeTurn();
		Player turn = getAppUtilities().getTurnOfPlayer();
		jLabels[10].setText(turn.getName());

		// reset timer control game
		this.timerControlGame.restart();
	}

	public void startNewGame(Player turn) {
		gameTimer.startCount();

		// clear play area and set new game
		playAreaPanel.clearScreen();

		// set timer control game
		this.timerControlGame = startChangeTurnIfDeadline();

		jLabels[8].setText(String.valueOf(getAppUtilities().getTotalSec()));
	}

	public void pausingGame() {
		gameTimer.stopCount();
		getAppUtilities().setGameStatus(false);
		if (timerControlGame != null) {
			timerControlGame.stop();
		}
	}

	private AppUtilities getAppUtilities() {
		return mFrame.getAppUtilities();
	}

	public void setAppUtilities(AppUtilities appUtilities) {
		appUtilities.getRuleOfGame().setAppUtilities(appUtilities);
		this.getPlayAreaPanel().getTableChess().setAppUtilities(appUtilities);
	}

	public Timer startChangeTurnIfDeadline() {
		int delay = getAppUtilities().getDeadLineTime() * 1000;
		ActionListener listener = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setPlayerTurn();
			}
		};

		if (timerControlGame == null) {
			this.timerControlGame = new Timer(delay, listener);
		}

		timerControlGame.start();
		return timerControlGame;
	}
}
